package com.hsytech.dijiebao.photos.adapters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.photos.entity.Image;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * 鍥剧墖Adapter
 */
public class ImageGridAdapter extends BaseAdapter {

	private static final int TYPE_CAMERA = 0;
	private static final int TYPE_NORMAL = 1;

	private Context mContext;

	private LayoutInflater mInflater;
	private boolean showCamera = true;
	private boolean showSelectIndicator = true;

	private List<Image> mImages = new ArrayList<Image>();
	private List<Image> mSelectedImages = new ArrayList<Image>();

	private int mItemSize;
	private GridView.LayoutParams mItemLayoutParams;

	public ImageGridAdapter(Activity context, boolean showCamera) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.showCamera = showCamera;
		mItemLayoutParams = new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT,
				GridView.LayoutParams.MATCH_PARENT);
	}

	/**
	 * 鏄剧ず閫夋嫨鎸囩ず鍣�?
	 *
	 * @param b
	 */
	public void showSelectIndicator(boolean b) {
		showSelectIndicator = b;
	}

	public boolean isShowCamera() {
		return showCamera;
	}

	public void setShowCamera(boolean b) {
		if (showCamera == b)
			return;

		showCamera = b;
		notifyDataSetChanged();
	}

	/**
	 * 閫夋嫨鏌愪釜鍥剧墖锛屾敼鍙橀�夋嫨鐘舵��?
	 *
	 * @param image
	 */
	public void select(Image image) {
		if (mSelectedImages.contains(image)) {
			mSelectedImages.remove(image);
		} else {
			mSelectedImages.add(image);
		}
		notifyDataSetChanged();
	}

	/**
	 * 閫氳繃鍥剧墖璺緞璁剧疆榛樿閫夋嫨
	 *
	 * @param resultList
	 */
	public void setDefaultSelected(ArrayList<String> resultList) {
		for (String path : resultList) {
			Image image = getImageByPath(path);
			if (image != null) {
				mSelectedImages.add(image);
			}
		}
		if (mSelectedImages.size() > 0) {
			notifyDataSetChanged();
		}
	}

	private Image getImageByPath(String path) {
		if (mImages != null && mImages.size() > 0) {
			for (Image image : mImages) {
				if (image.path.equalsIgnoreCase(path)) {
					return image;
				}
			}
		}
		return null;
	}

	/**
	 * 璁剧疆鏁版嵁闆�
	 *
	 * @param images
	 */
	public void setData(List<Image> images) {
		mSelectedImages.clear();

		if (images != null && images.size() > 0) {
			mImages = images;
		} else {
			mImages.clear();
		}
		notifyDataSetChanged();
	}

	/**
	 * 閲嶇疆姣忎釜Column鐨凷ize
	 *
	 * @param columnWidth
	 */
	public void setItemSize(int columnWidth) {

		if (mItemSize == columnWidth) {
			return;
		}

		mItemSize = columnWidth;

		mItemLayoutParams = new GridView.LayoutParams(mItemSize, mItemSize);

		notifyDataSetChanged();
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		if (showCamera) {
			return position == 0 ? TYPE_CAMERA : TYPE_NORMAL;
		}
		return TYPE_NORMAL;
	}

	@Override
	public int getCount() {
		return showCamera ? mImages.size() + 1 : mImages.size();
	}

	@Override
	public Image getItem(int i) {
		if (showCamera) {
			if (i == 0) {
				return null;
			}
			return mImages.get(i - 1);
		} else {
			return mImages.get(i);
		}
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {

		int type = getItemViewType(i);
		if (type == TYPE_CAMERA) {
			view = mInflater.inflate(R.layout.list_item_camera, viewGroup, false);
			view.setTag(null);
		} else if (type == TYPE_NORMAL) {
			ViewHolder holde;
			if (view == null) {
				view = mInflater.inflate(R.layout.list_item_image, viewGroup, false);
				holde = new ViewHolder(view);
			} else {
				holde = (ViewHolder) view.getTag();
				if (holde == null) {
					view = mInflater.inflate(R.layout.list_item_image, viewGroup, false);
					holde = new ViewHolder(view);
				}
			}
			if (holde != null) {
				holde.bindData(getItem(i));
			}
		}

		/** Fixed View Size */
		GridView.LayoutParams lp = (GridView.LayoutParams) view.getLayoutParams();
		if (lp.height != mItemSize) {
			view.setLayoutParams(mItemLayoutParams);
		}

		return view;
	}

	class ViewHolder {
		ImageView image;
		ImageView indicator;

		ViewHolder(View view) {
			image = (ImageView) view.findViewById(R.id.image);
			indicator = (ImageView) view.findViewById(R.id.checkmark);
			view.setTag(this);
		}

		void bindData(final Image data) {
			if (data == null)
				return;
			// 澶勭悊鍗曢�夊拰澶氶�夌姸鎬�?
			if (showSelectIndicator) {
				indicator.setVisibility(View.VISIBLE);
				if (mSelectedImages.contains(data)) {
					// 璁剧疆閫変腑鐘舵��
					indicator.setImageResource(R.mipmap.blue_yes);
				} else {
					// 鏈�夋�?
					indicator.setImageResource(R.mipmap.gary_no);
				}
			} else {
				indicator.setVisibility(View.GONE);
			}
			File imageFile = new File(data.path);

			if (mItemSize > 0) {
				// 鏄剧ず鍥剧墖
				Picasso.with(mContext).load(imageFile).placeholder(R.mipmap.ic_launcher)
						// .error(R.drawable.default_error)
						.resize(mItemSize, mItemSize).centerCrop().into(image);
			}
		}
	}

}
